import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.microsoft import EdgeChromiumDriverManager

# #编写自动化脚本，具体步骤
# # 1.打开浏览器 ---- 驱动管理
# EdgeIns=EdgeChromiumDriverManager().install()
# ### 创建Edge浏览器驱动对象
# driver = webdriver.Edge(service=Service(EdgeIns))
# time.sleep(2)
# # 2.输入百度网址：https://www.baidu.com（网址一定要完整）
# driver.get("https://www.baidu.com")
# time.sleep(2)
# # 3.找到百度输入框，输入关键词“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# time.sleep(2)
# # 4.点击“百度一下”按钮
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# time.sleep(2)
# # 5.关闭浏览器
# driver.quit()


# 元素定位
# 1.打开浏览器 ---- 驱动管理
EdgeIns=EdgeChromiumDriverManager().install()
# 创建浏览器参数配置
options = webdriver.EdgeOptions()
# 添加无头模式
# options.add_argument("-headless")

# 添加页面加载策略
# options.page_load_strategy = 'normal'
# options.page_load_strategy = 'eager'
options.page_load_strategy = 'none'

### 创建Edge浏览器驱动对象
driver = webdriver.Edge(service=Service(EdgeIns), options=options)
driver.get("https://haokan.baidu.com")
print(driver.title)
driver.quit()

# driver.get("https://www.baidu.com")
# driver.get("https://top.baidu.com/board?platform=pc&sa=pcindex_entry")
# time.sleep(3)

# ret = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text) # 获取每个元素对应的文本信息

# 点击事件
# 查找元素 + 点击元素
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# ele = driver.find_element(By.CSS_SELECTOR, "#su")
# ele.click()

# driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title").click()

# 查找元素 + 输入文本
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("helloworld")
# ele = driver.find_element(By.CSS_SELECTOR, "#kw")
# ele.send_keys("helloworld")

# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("hello")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,"#kw").clear();
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("world")
# time.sleep(3)

# # 查找文本信息
# text = driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title").text
# print(text)
# assert text == "city不city？来这儿最unhappy" # 通过断言来判断文本是否读取错误

# 获取“百度一下”按钮上的文字
# text = driver.find_element(By.CSS_SELECTOR, "#su").text
# text = driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print(text) # 没有打印任何文本信息，说明text为空

# # 获取页面标题和url
# title = driver.title
# url = driver.current_url
#
# print(title)
# print(url)

# #点击百度首页的图片功能，跳转到百度图片页面
# print("before:" + driver.title) # 百度首页的标题
# print("before:" + driver.current_url) # 百度首页的url
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# # 获取当前页面的句柄 -- 第一个标签页
# curHandle = driver.current_window_handle
# # 获取所有句柄
# allHandle = driver.window_handles
# # 遍历所有句柄，切换到新的页面
# for handle in allHandle:
#     if handle != curHandle:
#         # 切换句柄
#         driver.switch_to.window(handle)
#         # selenium中切换窗口要用这个新的方法，之前的switch_to_window已经被淘汰
#
# # 测试跳转结果
# print("after:" + driver.title) # 百度图片的标题
# print("after:" + driver.current_url) # 百度图片的url

# print("before:" + driver.title) # 百度热搜首页的标题
# print("before:" + driver.current_url) # 百度热搜首页的url
# driver.find_element(By.CSS_SELECTOR, "#sanRoot > header > div.container.tab-wrap_23sEv > div.c-theme-color.tabs-wrap_3Ac9n > a:nth-child(3) > span").click()
# time.sleep(3)
# print("after:" + driver.title) # 百度热搜小说的标题
# print("after:" + driver.current_url) # 百度热搜小说的url

# # 设置窗口大小
# # 最大化
# time.sleep(2)
# driver.maximize_window()
# # 最小化
# time.sleep(2)
# driver.minimize_window()
# # 全屏
# time.sleep(2)
# driver.fullscreen_window()
# # 手动设置尺寸
# time.sleep(2)
# driver.set_window_size(1024, 800)
# time.sleep(2)

# 添加屏幕截图
# filename = "./test.png"
# filename = "./images/test.png"
# driver.save_screenshot(filename)

# 进阶版本：每次生成的图片文件都不一样
# 百度首页
# autotest-2024-07-13-151733.png
# filename="autotest-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
# # ./images/autotest-2024-07-13-151733.png
# driver.save_screenshot("./images/" + filename)

# # 打开新的标签页：百度图片
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# time.sleep(3)
# driver.close()
#
# time.sleep(3)

# # 警告弹窗
# driver.get("file:///D:/%E8%B5%84%E6%96%99/C%20Primer%20Plus/%E9%B9%8F%E5%93%A5/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6%E7%AD%89%E6%96%87%E4%BB%B6/selenium-html/alert.html")
# time.sleep(2)
# # 点击调起弹窗
# driver.find_element(By.CSS_SELECTOR, "#tooltip").click()
#
# # 切换到弹窗
# alert = driver.switch_to.alert
# time.sleep(2)
# # 确定/取消
# alert.accept()()
# # alert.dismiss()
# time.sleep(2)
# # 弹窗关闭之后才能执行下面的代码
# driver.find_element(By.CSS_SELECTOR, "#tooltip").click()


# # 确认弹窗
# driver.get("file:///D:/%E8%B5%84%E6%96%99/C%20Primer%20Plus/%E9%B9%8F%E5%93%A5/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6%E7%AD%89%E6%96%87%E4%BB%B6/selenium-html/confirm.html")
# driver.find_element(By.CSS_SELECTOR, "body > input[type=button]").click()
# time.sleep(2)
# # 切换弹窗
# alert = driver.switch_to.alert
# # 确定/取消
# # alert.accept()
# alert.dismiss()
# time.sleep(2)


# # 提示弹窗 -- 输入框
# driver.get("file:///D:/%E8%B5%84%E6%96%99/C%20Primer%20Plus/%E9%B9%8F%E5%93%A5/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6%E7%AD%89%E6%96%87%E4%BB%B6/selenium-html/Prompt.html")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "body > input[type=button]").click()
# time.sleep(2)
# # 输入文本 + 确定 + 取消
# alert = driver.switch_to.alert
# alert.send_keys("Elena")
# time.sleep(2)
# # alert.accept()
# alert.dismiss()
# time.sleep(2)


# 添加隐式等待
# driver.implicitly_wait(3)
#
# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
#
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# 添加显示等待
# 创建显示等待类对象
# wait = WebDriverWait(driver, 3)
# # wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")))
# driver.get("file:///D:/%E8%B5%84%E6%96%99/C%20Primer%20Plus/%E9%B9%8F%E5%93%A5/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6%E7%AD%89%E6%96%87%E4%BB%B6/selenium-html/alert.html")
# driver.find_element(By.CSS_SELECTOR, "#tooltip")
# # 等待弹窗是否出现
# wait.until(EC.alert_is_present())
# driver.switch_to.alert.accept()

# driver.get("https://www.baidu.com")
# driver.implicitly_wait(10)
# wait = WebDriverWait(driver, 15)
# start = time.time() # 保存当前时间（时间戳）
# try:
#     res = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="haaaaaotsearch-content-wrapper"]/li[1]/a')))
# except:
#     end = time.time() # 保存当前时间（时间戳）
#     print("no such element")
# driver.quit()
# print(end-start)

# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# time.sleep(2)
# # 后退：推到百度首页
# driver.back()
# time.sleep(2)
# # 前进：退回到“迪丽热巴”关键词页面
# driver.forward()
# time.sleep(2)
# # 刷新
# driver.refresh()
# time.sleep(2)

# driver.get("file:///D:/%E8%B5%84%E6%96%99/C%20Primer%20Plus/%E9%B9%8F%E5%93%A5/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6%E7%AD%89%E6%96%87%E4%BB%B6/selenium-html/upload.html")
# driver.find_element(By.CSS_SELECTOR, "body > div > div > input[type=file]").send_keys("D:\\test.txt")
# time.sleep(3)
# driver.quit()